﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Outbound
{
    public partial class SelectPart : System.Web.UI.Page
    {
        private string ItemCode
        {
            get
            {
                object obj = ViewState["ItemCode"];
                return obj != null ? obj.ToString() : string.Empty;
            }
            set
            {
                ViewState["ItemCode"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ItemCode = Request.QueryString["itemCode"];
                BindGridView();
            }
        }

        private void BindGridView()
        {
//            string sql = @"SELECT T0.ItemCode,DistNumber
//                            FROM OSRQ T0
//                            JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
//                            JOIN OSRN T2 ON T2.SysNumber=t0.SysNumber
//                            WHERE T1.U_Class1=103 AND U_Class2=206 and t0.quantity>0";

//            string sql = @"SELECT T0.ItemCode,DistNumber
//                            FROM OSRQ T0
//                            JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
//                            JOIN OSRN T2 ON T2.SysNumber=T0.SysNumber
//                            WHERE T1.U_Class1=103 AND U_Class2=206 and T0.Quantity>0 AND T0.ItemCode=@ItemCode
//                            ";

            string sql = @"SELECT T0.ItemCode,DistNumber                            FROM OSRQ T0                            JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode                            JOIN OSRN T2 ON T2.SysNumber=T0.SysNumber AND T2.ItemCode=T0.ItemCode                            WHERE T1.U_Class1=103 AND U_Class2=206 and T0.Quantity>0 AND T0.ItemCode=@ItemCode";

            Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@ItemCode", DbType.String, this.ItemCode);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string itemCode = gridView.DataKeys[gridViewRow.RowIndex]["ItemCode"].ToString();
            string distNumber = gridView.DataKeys[gridViewRow.RowIndex]["DistNumber"].ToString();

            switch (e.CommandName)
            {
                case "CmdSelect":
                    string returnValue = itemCode + "|" + distNumber;
                    Response.Write("<script>window.returnValue='" + returnValue + "';window.opener = null;window.open('', '_self', '');window.close();</script>");
                    break;
                default:
                    break;
            }
        }
    }
}